package ren.steve.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;
import ren.steve.model.system.bean.SystemMenu;
import ren.steve.model.system.bean.SystemRoleMenu;
import ren.steve.model.system.bean.SystemUser;
import ren.steve.model.system.vo.LoginVo;
import ren.steve.system.custom.CustomUser;
import ren.steve.system.service.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Component
public class UserDetailServiceImpl implements UserDetailsService {

  @Autowired
  private SystemUserRoleService systemUserRoleService;

  @Autowired
  private SystemRoleMenuService systemRoleMenuService;

  @Autowired
  private SystemMenuService systemMenuService;

  @Autowired
  private SystemUserService systemUserService;

  @Autowired
  private LoginService loginService;


  @Override
  public UserDetails loadUserByUsername(String account) throws UsernameNotFoundException {
    LoginVo loginVo = new LoginVo();
    loginVo.setAccount(account);
    SystemUser user = loginService.login(loginVo);
    if (user == null) {
      throw new UsernameNotFoundException("用户不存在");
    }
    if (Objects.equals(user.getStatus(), "999")) {
      throw new RuntimeException("用户已禁用");
    }
    // 根据 UserId 查询操作权限数据列表
    List<String> roles = systemUserRoleService.getSystemRoleByUserId(user.getId());
    // 根据 User Role 关联表获取用户角色
    // 转换为 Security 要求的格式数据
    List<SimpleGrantedAuthority> authorities = new ArrayList<>();
    List<String> menuIds = new ArrayList<>();
    boolean isDeveloper = false;

    for (String role : roles) {
      if (role.equals("1")) {
        isDeveloper = true;
      }
      List<SystemRoleMenu> menus = systemRoleMenuService.getSystemMenuByRoleId(role);
      if (menus != null) {
        for (SystemRoleMenu menu : menus) {
          if (!menuIds.contains(menu.getMenuId())) {
            menuIds.add(menu.getMenuId());
          }
        }
      }
    }
    System.out.println(isDeveloper);
    List<SystemMenu> systemMenus = new ArrayList<>();
    if (isDeveloper) {
      QueryWrapper<SystemMenu> wrapper = new QueryWrapper<>();
      wrapper.eq("type", 3);
      systemMenus = systemMenuService.list(wrapper);
    } else {
      systemMenus = systemMenuService.listByIds(menuIds);
    }
    for (SystemMenu systemMenu : systemMenus) {
      if (!Objects.equals(systemMenu.getPath(), "") && systemMenu.getPath() != null) {
        System.out.println(systemMenu.getPath());
        authorities.add(new SimpleGrantedAuthority(systemMenu.getPath().trim()));
      }
    }
    // return
    return new CustomUser(user, authorities);
  }
}

